$(function(){
	queryUrlsData(1);
	$("#importGds").click(function(){
		window.top.location.href =WEB_ROOT+"/gds/gds_import_init";
	});

	$("#exportGds").click(function(){
		var batchId = $.trim($("#batchId").val());
		var state = $.trim($("#state").val());
		var gdsName = $.trim($("#gdsName").val());
		var gdsId = $.trim($("#gdsid").val());
		var brand = $.trim($("#brand").val());
		var url = WEB_ROOT+"/gds/gds_export";
		var form = $("<form>");
		form.attr('style','display:none');
		form.attr('target','');
		form.attr('method','post');
		form.attr('action',url);
		$("<input type='text' name='batchId' value='"+batchId+"' />").appendTo(form);
		$("<input type='text' name='state' value='"+state+"' />").appendTo(form);
		$("<input type='text' name='gdsName' value='"+gdsName+"' />").appendTo(form);
		$("<input type='text' name='gdsId' value='"+gdsId+"' />").appendTo(form);
		$("<input type='text' name='brand' value='"+brand+"' />").appendTo(form);
		$('body').append(form);
		form.submit();
		form.remove();
	});

	//删除商品
	$("#delGds").click(function(){
		var idLst = [];
		$('input[name=gdsSelect]:checkbox:checked').each(function(){
			idLst.push($(this).val());
		});
		if (idLst.length == 0) {
			eDialog.alert('请选择记录!');
			return;
		}else{
			eDialog.confirm("您确认要删除选中的商品吗？", {
				buttons : [{
					caption : '确认',
					callback : function(){
						eDialog.alert('success','保存成功！');
						$.ajax({
							url: GLOBAL.WEBROOT+"/gds/delGdsInfo",
							data:[{name:'idLst',value:idLst}],
							type:"post",
							dataType:"json",
							async:true,
							cache:true,
							success: function (data){
								//从后台中获取到数据添加到树结构中
								if(data.flag == "success"){
									queryUrlsData(1);
								}
							},
							error:function(XMLHttpRequest, textStatus, errorThrown){
							}
						});
					}
				},{
					caption : '取消',
					callback : $.noop
				}]
			});
		}
	});
	//编辑商品
	$("#editGds").click(function(){
		var idLst = [];
		$('input[name=gdsSelect]:checkbox:checked').each(function(){
			idLst.push($(this).val());
		});
		if (idLst.length == 0) {
			eDialog.alert('请选择记录!');
			return;
		}else if(idLst.length > 1){
			eDialog.alert('请选择且只选择一条记录!');
			return;
		}
		var id = idLst[0];
		if(id){
			window.location.href=GLOBAL.WEBROOT+"/gds/editGdsInfo?id="+id;
		}else{
			eDialog.alert('请选择记录');
			return;
		}
	});
});
function queryUrlsData(pageNo){
    var params = {
		batchId:$.trim($("#batchId").val()),
		state:$.trim($("#state").val()),
		gdsName:$.trim($("#gdsName").val()),
		gdsId:$.trim($("#gdsid").val()),
		brand:$.trim($("#brand").val()),
        pageNo:pageNo,
        pageSize:50
    };

    ajaxLoadPage(params,WEB_ROOT+"/gds/gdsList",$("#gdslist-div-id"),function(){
        $("#urlPagerId").pager({callback:function(pageNo){
            queryUrlsData(pageNo);
        }});
		$('#checkAll').on('click', function(){
			if($(this).is(':checked')) {
				$('input[name=gdsSelect]').prop('checked', 'true');
			} else {
				$('input[name=gdsSelect]').removeAttr('checked');
			}
		});
    });
}